//
//  CategoryGridView.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2017/5/23.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

#import "CategoryGridView.h"
#import "UIImage+CZExtend.h"

#define kCategoryGridColumnCount 3
#define kCategoryGridRowHeight   50.0f
#define kCategoryGridCellTag     100
#define SINGLE_LINE_WIDTH        (1 / [UIScreen mainScreen].scale)

@interface CategoryGridView ()
@property (nonatomic,strong) NSArray *categorySource;
@end

@implementation CategoryGridView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setDataSource:(NSArray *)source{
    if (_dataSource!=source) {
        _dataSource = source;
        
        NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:0];
        if (source && [source count]>0) {
            
            NSInteger row =([source count]-1)%kCategoryGridColumnCount==0?([source count]-1)/kCategoryGridColumnCount:(([source count]-1)/kCategoryGridColumnCount +1);
            NSInteger total = row * kCategoryGridColumnCount +1;
            [resultArray addObjectsFromArray:source];
            
            for (NSInteger i=total-[source count]; i>0; i--) {
                [resultArray addObject:@""];
            }
        }
        self.categorySource = resultArray;
        
        [self createControls];
    }
}

- (void)createControls{

    //移除所有子视图
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (self.categorySource && [self.categorySource count]>0) {
        
        NSInteger row = ([self.categorySource count]-1)/kCategoryGridColumnCount;
        CGFloat cellWith = self.bounds.size.width /(kCategoryGridColumnCount + 1)*1.0-SINGLE_LINE_WIDTH*2;
        CGFloat cellHeight = kCategoryGridRowHeight - 2*SINGLE_LINE_WIDTH;
        
        UIButton *btn1 = [self createButtonWithFrame:CGRectMake(SINGLE_LINE_WIDTH, SINGLE_LINE_WIDTH, cellWith, row*kCategoryGridRowHeight-2*SINGLE_LINE_WIDTH) title:self.categorySource[0] tag:kCategoryGridCellTag];
        [self addSubview:btn1];
        
        self.height = row*kCategoryGridRowHeight;
        
        for (NSInteger i=0; i<row; i++) {
            
            for (NSInteger j=0; j<kCategoryGridColumnCount; j++) {
                
                UIButton *btn = [self createButtonWithFrame:CGRectMake((cellWith+SINGLE_LINE_WIDTH)*(j+1), SINGLE_LINE_WIDTH*(i+1)+i*cellHeight, cellWith, cellHeight) title:self.categorySource[i*kCategoryGridColumnCount+j+1] tag:kCategoryGridCellTag+(i+1)+j];
                
                [self addSubview:btn];
            }
        }
    }
    
    [self setNeedsDisplay];

}

- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage createImageWithColor:[[UIColor grayColor] colorWithAlphaComponent:0.95]] forState:UIControlStateHighlighted];
    btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [btn addTarget:self action:@selector(categoryClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)categoryClick:(UIButton *)sender{
    
    
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextSetLineWidth(context,SINGLE_LINE_WIDTH); //SINGLE_LINE_WIDTH
    CGContextBeginPath(context);
    
    NSInteger row = ([self.categorySource count]-1)/kCategoryGridColumnCount;
    //顶部横线
    [self drawLineWithContext:context startPoint:CGPointMake(0, 0) endPoint:CGPointMake(self.width, 0)];
    //底部横线
    [self drawLineWithContext:context startPoint:CGPointMake(0, row*kCategoryGridRowHeight) endPoint:CGPointMake(self.width, row*kCategoryGridRowHeight)];
    //左侧横线
    [self drawLineWithContext:context startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, row*kCategoryGridRowHeight)];
    //右侧横线
    [self drawLineWithContext:context startPoint:CGPointMake(self.width, 0) endPoint:CGPointMake(self.width, row*kCategoryGridRowHeight)];
    
    CGFloat cellWith = self.bounds.size.width /(kCategoryGridColumnCount + 1)*1.0-SINGLE_LINE_WIDTH*2;;
    //竖线
    for (NSInteger j=1; j<kCategoryGridColumnCount+1; j++) {
        
        [self drawLineWithContext:context startPoint:CGPointMake((cellWith+SINGLE_LINE_WIDTH)*j, 0) endPoint:CGPointMake((cellWith+SINGLE_LINE_WIDTH)*j, row*kCategoryGridRowHeight-2*SINGLE_LINE_WIDTH)];
    }
    //横线
    CGFloat cellHeight = kCategoryGridRowHeight - 2*SINGLE_LINE_WIDTH;
    for (NSInteger i=1; i<row; i++) {
        [self drawLineWithContext:context startPoint:CGPointMake(cellWith+2*SINGLE_LINE_WIDTH, (cellHeight+SINGLE_LINE_WIDTH)*i) endPoint:CGPointMake(self.width-SINGLE_LINE_WIDTH, (cellHeight+SINGLE_LINE_WIDTH)*i)];
    }
    
    //CGContextClosePath(context);
    CGContextStrokePath(context);
}

- (void)drawLineWithContext:(CGContextRef)context startPoint:(CGPoint)startP endPoint:(CGPoint)endP{
    CGContextMoveToPoint(context, startP.x, startP.y);  //起点坐标
    CGContextAddLineToPoint(context, endP.x, endP.y);   //终点坐标
}

@end
